//Copyright 2011 New York City 3D Community

//This file is part of New York City 3D.

//New York City 3D is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//New York City 3D is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with New York City 3D.  If not, see <http://www.gnu.org/licenses/>.

package nyc3d.gui;

import com.jme3.system.AppSettings;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.CheckBox;
import de.lessvoid.nifty.controls.DropDown;
import de.lessvoid.nifty.controls.button.builder.CreateButtonControl;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.io.IOException;
import java.util.ArrayList;
import nyc3d.NYC3D;
import nyc3d.settings.Resolution;
import nyc3d.settings.SettingsUtility;

public class OptionsScreen implements ScreenController{
  private Nifty nifty;
  private Screen screen;
  private AppSettings settings = new AppSettings(true);
  ArrayList<Resolution> resolutionList;
  ArrayList<String> antialiasingList;
  private String sampleString;

  public void bind(final Nifty newNifty, final Screen newScreen) {
      int n;
      Resolution userRes;
      resolutionList = new ArrayList<Resolution>();
      resolutionList.add(new Resolution(800, 600));
      resolutionList.add(new Resolution(1024, 768));
      resolutionList.add(new Resolution(1280, 720));
      resolutionList.add(new Resolution(1920, 1080));

      antialiasingList = new ArrayList<String>();
      antialiasingList.add("Disabled");
      antialiasingList.add("2x");
      antialiasingList.add("4x");
      antialiasingList.add("8x");
      antialiasingList.add("16x");

    try{
            settings = SettingsUtility.parseSettingsFromFile("settings.nyc");
    } catch(IOException e){ }
      
      
      userRes = new Resolution(settings.getWidth(), settings.getHeight());
      System.out.println("User resolution: " + userRes);

      for(n = 0; n < resolutionList.size(); n++){
          if (userRes.toString().equals(resolutionList.get(n).toString()))
              break;
          else if (userRes.getWidth() < resolutionList.get(n).getWidth()){
              resolutionList.add(n, userRes);
              break;
          }
      }

      if (settings.getSamples() != 0){
          sampleString = Integer.toString(settings.getSamples()) + "x";
          if (!sampleString.equals(userRes.toString()))
          for(n = 1; n < antialiasingList.size(); n++){

              if (sampleString.equals(antialiasingList.get(n)))
                  break;
              else if (Integer.parseInt(sampleString.substring(0, sampleString.indexOf("x"))) < Integer.parseInt(antialiasingList.get(n).substring(0, antialiasingList.get(n).indexOf("x")))){
                  antialiasingList.add(n, sampleString);
                  break;
              }
          }
      }

      if (n == resolutionList.size())
          resolutionList.add(userRes);

    screen = newScreen;
    nifty = newNifty;
    

    DropDown dropDown1 = screen.findNiftyControl("resolution", DropDown.class);
    if (dropDown1 != null) {
      for(Resolution r: resolutionList){
          dropDown1.addItem(r.toString());
          
          if (r.equals(userRes))
              dropDown1.selectItem(r.toString());
        }
    }

    CheckBox fullscreenBox = screen.findNiftyControl("fullscreen", CheckBox.class);
    fullscreenBox.setChecked(settings.isFullscreen());
    
    
    DropDown antialiasing = screen.findNiftyControl("antialiasing", DropDown.class);
    if (antialiasing != null) {      
      
      for(String s: antialiasingList){
          antialiasing.addItem(s);

          if (settings.getSamples() == 0)
            antialiasing.selectItem("Disabled");
          else if (s.equals(sampleString))
              antialiasing.selectItem(s);
        }        
    }

    CheckBox vSyncBox = screen.findNiftyControl("vsync", CheckBox.class);
    vSyncBox.setChecked(settings.isVSync());

    CreateButtonControl createBackButton = new CreateButtonControl("backButton2");
    createBackButton.set("label", "Return Without Saving");
    createBackButton.setWidth("200px");
    createBackButton.setAlign("right");
    createBackButton.setInteractOnClick("returnWithoutSaving()");
    createBackButton.create(newNifty, screen, screen.findElementByName("buttonPanel"));
    
    CreateButtonControl createBackButtonSave = new CreateButtonControl("backButton");
    createBackButtonSave.set("label", "Return and Save");
    createBackButtonSave.setWidth("150px");
    createBackButtonSave.setAlign("right");
    createBackButtonSave.setInteractOnClick("returnAndSave()");
    createBackButtonSave.create(newNifty, screen, screen.findElementByName("buttonPanel"));
  }

  public void onStartScreen() {
  }

  public void onEndScreen() {
  }

  public void returnAndSave() {
    Resolution finalRes = Resolution.fromString(screen.findNiftyControl("resolution", DropDown.class).getSelection().toString());
    settings.setWidth(finalRes.getWidth());
    settings.setHeight(finalRes.getHeight());
    settings.setFullscreen(screen.findNiftyControl("fullscreen", CheckBox.class).isChecked());

    if (settings.isFullscreen())
        settings.setBitsPerPixel(32);

    if (screen.findNiftyControl("antialiasing", DropDown.class).getSelectedIndex() == 0)
        settings.setSamples(0);
    else
        settings.setSamples(Integer.parseInt(screen.findNiftyControl("antialiasing", DropDown.class).getSelection().toString().replace("x", "")));

    settings.setVSync(screen.findNiftyControl("vsync", CheckBox.class).isChecked());

    try{
        SettingsUtility.writeSettingsToFile("settings.nyc", settings);
    } catch(IOException e){}
    
     if (nifty.findPopupByName("popupRestart") == null)
        nifty.createPopupWithId("popupRestart", "popupRestart");
    
    nifty.showPopup(this.screen, "popupRestart", null);
    
    System.out.println("Popup should show up now.");
    
    NYC3D.desiredSettings = settings;
    NYC3D.restartDisplay = true;
  }
  
  public void exit() {
    System.exit(0);
  }
  
  public void resume(){
      returnWithoutSaving();
  }

  public void returnWithoutSaving(){
      nifty.fromXml("menu.xml", "menu");
    }
}